<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace home\controller;
use \temmoku\db;
use \temmoku\controller;
use \temmoku\lib\pages;
use \temmoku\lib\custom_model;
use \admin\public_class\article;
class articles extends controller
{

    public function articles()
    {
		
    	!isset($_GET['aid']) && $this->err('缺少必要参数');
    	!isset($_GET['page']) && $_GET['page']=1;
    	$floor=floor($_GET['aid']/1000);
    	$cache_dir=RUNTIME_PATH."/cache/".MODULE.'/'.DEVICE."/content/{$floor}/{$_GET['aid']}_{$_GET['page']}.txt";
    	if(C('content_cache_time') && is_file($cache_dir) && time()-@filemtime($cache_dir) < C('content_cache_time')*3600){
    		echo get_cache($cache_dir);
    		exit;
    	}
    	
    	$data=db::select('*')->from(jab.'article AS A')->innerJoin(jab.'content AS B','A.aid =B.aid')->where('A.aid='.$_GET['aid'])->orderByDESC(['A.aid'])->setPaging('1')->page($_GET['page'])->row();
		if(!$data){
    		$this->err('不存在的数据');
   		}
   		db::query("UPDATE `".jab."article` SET `clicks` = `clicks`+1 WHERE aid=$data[aid]");
   		//判断是不是模型
   		if($data['mid']){
   			$Custom_Model=db::select('*')->from(jab.'content_'.$data['mid'])->where("aid= '$data[aid]' ")->row();
   			$setting=C('home|home_MODELDB.'.$data['mid'])['field'];
   			$_data=(new Custom_Model)->show($setting,$Custom_Model);
   			$data=array_merge((array)$data,(array)$_data);
   		}
   		$CATEGORY_DB=c('home|CATEGORY_SETTING.'.$data['cid']);
   		$CATEGORY_SETTING=unserialize($CATEGORY_DB['setting']);

   		if($CATEGORY_SETTING['tpl']['article']){
   			$tpl=$CATEGORY_SETTING['tpl']['article'];
   		}else{
   			if($data['mid']){
   				$tpl="articles_".$data['mid'];
   			}else{
   				$tpl="articles";
   			}
   		}
   		//判断是否有浏览权限
   		if($CATEGORY_SETTING['look'] && C('mydb.groupid') != '3'){
   			if(!in_array(C('mydb.groupid'),$CATEGORY_SETTING['look'])){
   				$this->err('没有权限');
   			}
   		}
   		
   		//判断是否出售
   		if($data['sell_jifen'] && C('mydb.groupid') != '3'){
   			if($_POST['step']=='payment'){
   				if(!C('MYDB')){
   					$this->err('请登陆');
   				}
   				if($data['sell_jifen'] > C('mydb.jifen')){
   					$this->err('你的积分不足以支付此篇文章');
   				}
   				//减少购买者积分
   				(new \temmoku\lib\integral)->add(C('mydb.uid'),-$data['sell_jifen'],"购买文章所付积分$data[sell_jifen]文章地址：".U(['aid'=>$data['aid'],'cid'=>$data['cid']],MODULE));
   				//增加出售者积分
   				(new \temmoku\lib\integral)->add($data['uid'],$data['sell_jifen'],"出售文章所赚积分$data[sell_jifen]文章地址：".U(['aid'=>$data['aid'],'cid'=>$data['cid']],MODULE));
   				
   				$arr=[
   					'aid'=>$data['aid'],
					'uid'=>C('mydb.uid'),
					'username'=>C('mydb.username'),
					'integral'=>$integral,
					'posttime'=>NOWTIME
				];
				db::insert(jab.'buyarticle')->cols($arr)->query();
   				$this->success('支付成功');
   			}
   			$_data='';
   			if(C('mydb')){
   				$_data=db::select('*')->from(jab.'buyarticle')->where("aid = '$data[aid]' AND uid=".C('mydb.uid'))->row();
   			}
   			if(!$_data){
   				$data['content']=(new article)->sell_jifen($data['sell_jifen']);
   			}
   		}
   		
   		//判断是否加密
   		if($data['password'] && C('mydb.groupid') != '3'){
   			if($_POST['step']=='pass'){
   				if(md5($_POST['password']) != $data['password']){
   					$data['content']=(new article)->pass('密码不正确，请重新输入');
   				}else{
   					(new \temmoku\lib\cookie)->set('article_'.$data['aid'],$data['password'],'7');
   				}
   			}else{
   				$article=(new \temmoku\lib\cookie)->get('article_'.$data['aid']);
   				if(!$article){
   					$data['content']=(new article)->pass();
   				}
   			}
   		}
   		
    	$Get_Page=(new Pages)->Pages(jab.'content','aid='.$_GET['aid'],'','1',$_GET['page']);
    	$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {return U(['aid'=>$_GET['aid'],'cid'=>$_GET['cid'],'page'=>$matches[1]],MODULE);}, $Get_Page );
    	//title标签
    	
    	$CATEGORY_NAME=empty($CATEGORY_DB['seo_name']) ? $CATEGORY_DB['name'] :$CATEGORY_DB['seo_name'];
    	$title=empty($data['seotitle']) ? $data['title'] :$data['seotitle'];
    	$TITLE=$title.'_'.$CATEGORY_NAME.'_'.C('WEBNAME');
    	//关键字标签
    	$_keyword=empty($data['keyword']) ? C('WEB_SEO_KEYWORDS') :$data['keyword'];
    	if($_keyword){
    		$keyword=implode(',',$_keyword=explode(' ' ,$_keyword));
    		$data['keyword']='';
    		foreach($_keyword AS $__keyword){
    			$data['keyword'] .='<a href="">'.$__keyword.'</a>';
    		}
    	}

		
    	//描述标签
    	$DESCRIPTION=$data['description'];
    	$data['thumb']=get_img_url($data['thumb']);
    	$data['content']=htmlspecialchars_decode(str_replace  ( 'WWW_TEMMOKU_CN'  , WEBURL.'/'.C('upfiles_Catalog') ,  $data['content']));
    	//最新新闻
    	$data['posttime']=date("Y-m-d H:i:s",$data['posttime']);
		$Crumbs = preg_replace_callback('/cid=([0-9]+)/',function ($matches) {return U(['cid'=>$matches[1]],MODULE);}, C('home|HOME_CRUMBS.'.$data['cid']) );
    	$this->assign('WEBNAME',$TITLE);
    	$this->assign('KEYWORDS',$keyword);
    	$this->assign('DESCRIPTION',$DESCRIPTION);
    	$this->assign('data',$data);
    	$this->assign('Get_Page',$Get_Page);
    	$this->assign('Crumbs',$Crumbs);
		//不是pc设备才去获取
		if(DEVICE !='pc'){
			if(DEVICE=='wechat'){//如果是微信打开
				$wechat_share=(new \plugin\wechat\public_class\wechat)->getticket(U(['aid'=>$data['aid'],'cid'=>$data['cid']]));
				$this->assign('wechat_share',$wechat_share);
			}
				$wechat_share=(new \plugin\wechat\public_class\wechat)->getticket(U(['aid'=>$data['aid'],'cid'=>$data['cid']]));
				$this->assign('wechat_share',$wechat_share);
				
				
			$menu_more_list=db::select('*')->from(jab.'category')->where("upcid='{$CATEGORY_DB['upcid']}'")->query();
			$this->assign('menu_more_list',$menu_more_list);
			$this->assign('webname',$CATEGORY_DB['name']);
		}
        $this->display(tpl($tpl));
        
        if(C('content_cache_time') && !$data['password'] && !$data['sell_jifen']){
    		$content=ob_get_contents();
    		$content .="<script type=\"text/javascript\" src=\"".WEBURL."/articles/content_update?aid=$data[aid]\" ></script>";
			write_file($cache_dir,$content);
		}
    }
    
    public function content_update(){
    	$data=db::select('*')->from(jab.'article')->where("aid= '$_GET[aid]'")->row();
    	if($data){
    		db::query("UPDATE `".jab."article` SET `clicks` = `clicks`+1 WHERE aid=$data[aid]");
    		$clicks=$data['clicks']+1;
    		echo "$('#clicks').html($clicks);$('#comment_num').html($data[comment_num]);allnum='$data[comment_num]'";
    	}
    }
}
?>